package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.vehicle.mapper.VehicleInspectionMapper;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleInspectionQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.entity.VehicleInspection;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleInspectionVO;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleInspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class VehicleInsepectionServiceImpl implements VehicleInspectionService {

    @Autowired
    private VehicleInspectionMapper vehicleInspectionMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<VehicleInspectionVO> selectvehicle(VehicleInspectionQuery query) {
        List<VehicleInspectionVO> vos = vehicleInspectionMapper.select(query);
        for (VehicleInspectionVO vehicleInspectionVO : vos){
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setId(vehicleInspectionVO.getVehicleId());
            List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
            for (VehicleVO vehicleVO : vehicleVOS) {
                vehicleInspectionVO.setLicense(vehicleVO.getLicense());
            }
        }
        return vos;
    }

    @Override
    public void insert(VehicleInspection vehicleInspection) {
        Vehicle vehicle = new Vehicle();
        if(vehicleInspection.getStatus().equals("1")){
            vehicle.setId(vehicleInspection.getVehicleId());
            vehicle.setStatus("4");//设置车辆的状态为送检中
        }else {
            vehicle.setId(vehicleInspection.getVehicleId());
            vehicle.setStatus("1");//设置车辆状态为空闲
        }
        vehicleMapper.update(vehicle);
        if (vehicleInspection.getId()==null){
            vehicleInspection.setCreateTime(new Date());
            vehicleInspectionMapper.insert(vehicleInspection);
        }else {
            vehicleInspectionMapper.update(vehicleInspection);
        }

    }

    @Override
    public void deleteVehicleById(Long id) {
        vehicleInspectionMapper.deleteVehicleById(id);
    }
}
